/**
 * This class represents the player configuration window that appears when a new
 * game is started, and allows assignment of name, selection of difficulty, and
 * allocation of skill points. Data collected from this class is used to create
 * a new Player object.
 * 
 * @author Stanley Ayzenberg, Araya Zaesim, Michelle Partogi, Adam Perez
 * @version 1.0
 */


import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
 * @version 1.0
 */
public class ConfigWindow {

	/**
	 * Method toString.
	
	 * @return String */
	public String toString() {
		return "";
	}

	/**
	 * Field frame.
	 */
	private JFrame frame;

	/**
	 * Field isFalse
	 */
	private static final boolean ISFALSE = false;

	/**
	 * Field panel.
	 */
	private JPanel panel;

	/**
	 * Field lblPlayerName.
	 */
	private JLabel lblPlayerName;

	/**
	 * Field nameField.
	 */
	private JTextField nameField;

	/**
	 * Field horizontalGlue.
	 */
	private Component horizontalGlue;

	/**
	 * Field verticalBox.
	 */
	private Box verticalBox;

	/**
	 * Field horizontalBox.
	 */
	private Box horizontalBox;

	/**
	 * Field horizontalBox_1.
	 */
	private Box horizontalBox_1;

	/**
	 * Field label.
	 */
	private JLabel label;

	/**
	 * Field horizontalGlue_1.
	 */
	private Component horizontalGlue_1;

	/**
	 * Field textField_1.
	 */
	private JTextField textField_1;

	/**
	 * Field lblDifficulty.
	 */
	private JLabel lblDifficulty;

	/**
	 * Field rdbtnEasy.
	 */
	private JRadioButton rdbtnEasy;

	/**
	 * Field rdbtnBeginner.
	 */
	private JRadioButton rdbtnBeginner;

	/**
	 * Field rdbtnNormal.
	 */
	private JRadioButton rdbtnNormal;

	/**
	 * Field rdbtnHard.
	 */
	private JRadioButton rdbtnHard;

	/**
	 * Field rdbtnImpossible.
	 */
	private JRadioButton rdbtnImpossible;

	/**
	 * Field horizontalGlue_2.
	 */
	private Component horizontalGlue_2;

	/**
	 * Field lblSkills.
	 */
	private JLabel lblSkills;

	/**
	 * Field horizontalBox_3.
	 */
	private Box horizontalBox_3;

	/**
	 * Field lblPilot.
	 */
	private JLabel lblPilot;

	/**
	 * Field horizontalGlue_4.
	 */
	private Component horizontalGlue_4;

	/**
	 * Field horizontalBox_4.
	 */
	private Box horizontalBox_4;

	/**
	 * Field pilotPointsLabel.
	 */
	private JLabel pilotPointsLabel;

	/**
	 * Field button.
	 */
	private JButton button;

	/**
	 * Field button_1.
	 */
	private JButton button_1;

	/**
	 * Field horizontalBox_5.
	 */
	private Box horizontalBox_5;

	/**
	 * Field lblEngineer.
	 */
	private JLabel lblEngineer;

	/**
	 * Field button_2.
	 */
	private JButton button_2;

	/**
	 * Field engineerPointsLabel.
	 */
	private JLabel engineerPointsLabel;

	/**
	 * Field button_3.
	 */
	private JButton button_3;

	/**
	 * Field horizontalBox_6.
	 */
	private Box horizontalBox_6;

	/**
	 * Field lblTrader.
	 */
	private JLabel lblTrader;

	/**
	 * Field button_4.
	 */
	private JButton button_4;

	/**
	 * Field traderPointsLabel.
	 */
	private JLabel traderPointsLabel;

	/**
	 * Field button_5.
	 */
	private JButton button_5;

	/**
	 * Field horizontalBox_7.
	 */
	private Box horizontalBox_7;

	/**
	 * Field lblFighter.
	 */
	private JLabel lblFighter;

	/**
	 * Field button_6.
	 */
	private JButton button_6;

	/**
	 * Field fighterPointsLabel.
	 */
	private JLabel fighterPointsLabel;

	/**
	 * Field button_7.
	 */
	private JButton button_7;

	/**
	 * Field horizontalGlue_3.
	 */
	private Component horizontalGlue_3;

	/**
	 * Field horizontalGlue_5.
	 */
	private Component horizontalGlue_5;

	/**
	 * Field horizontalGlue_6.
	 */
	private Component horizontalGlue_6;

	/**
	 * Field horizontalGlue_7.
	 */
	private Component horizontalGlue_7;

	/**
	 * Field rigidArea.
	 */
	private Component rigidArea;

	/**
	 * Field rigidArea_1.
	 */
	private Component rigidArea_1;

	/**
	 * Field rigidArea_2.
	 */
	private Component rigidArea_2;

	/**
	 * Field rigidArea_3.
	 */
	private Component rigidArea_3;

	/**
	 * Field skillPointLabels.
	 */
	private JLabel skillPointLabels;

	/**
	 * Field btnMakeCharacter.
	 */
	private JButton btnMakeCharacter;

	/**
	 * Field skillPoints.
	 */
	private int skillPoints;

	/**
	 * Field pilotPoints.
	 */
	private int pilotPoints;

	/**
	 * Field fighterPoints.
	 */
	private int fighterPoints;

	/**
	 * Field traderPoints.
	 */
	private int traderPoints;

	/**
	 * Field engineerPoints.
	 */
	private int engineerPoints;

	/**
	 * Field player.
	 */
	private Player player;

	/**
	 * Field gui.
	 */
	public SpaceTraderGui gui = null;
	
	/**
	 * Constant bound1 for the window
	 */
	private static final int BOUNDONE = 100;
	
	/**
	 * Constant bound2 for the window
	 */
	private static final int BOUNDTWO = 100;
	
	/**
	 * Constant bound3 for the window
	 */
	private static final int BOUNDTHREE = 450;
	
	/**
	 * Constant bound4 for the window
	 */
	private static final int BOUNDFOUR = 350;
	
	/**
	 * Constant flowbounds for layout.
	 */
	private static final int FLOWBOUNDS = 5;
	
	/**
	 * Constant max skillpoints.
	 */
	private static final int MAX_SKILLPOINTS = 16;
	
	/**
	 * Constant field columns for text fields.
	 */
	private static final int FIELD_COLUMNS = 10;
	
	/**
	 * Constant Dimension x for rigid boxes.
	 */
	private static final int RIGIDDIMENSIONONE = 200;
	
	/**
	 * Constant Dimension y for rigid boxes.
	 */
	private static final int RIGIDDIMENSIONTWO = 10;
	
	/**
	 * Constant initial credits to start with.
	 */
	private static final int STARTING_CREDITS = 1000;
	
	/**
	 * Create the application.
	 * @param gui SpaceTraderGui
	 */
	public ConfigWindow(SpaceTraderGui gui) {
		this.gui = gui;
		initialize();
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(BOUNDONE, BOUNDTWO, BOUNDTHREE, BOUNDFOUR);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new FlowLayout(
				FlowLayout.LEFT, FLOWBOUNDS, FLOWBOUNDS));

		/* Initial allocation of skill points (16 to use) */
		skillPoints = MAX_SKILLPOINTS;
		pilotPoints = 0;
		fighterPoints = 0;
		traderPoints = 0;
		engineerPoints = 0;

		panel = new JPanel();
		frame.getContentPane().add(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		verticalBox = Box.createVerticalBox();
		panel.add(verticalBox);

		label = new JLabel("Player Name");

		horizontalGlue_1 = Box.createHorizontalGlue();

		textField_1 = new JTextField();
		textField_1.setColumns(FIELD_COLUMNS);

		horizontalBox = Box.createHorizontalBox();
		verticalBox.add(horizontalBox);

		lblPlayerName = new JLabel("Name");
		horizontalBox.add(lblPlayerName);

		horizontalGlue = Box.createHorizontalGlue();
		horizontalBox.add(horizontalGlue);

		nameField = new JTextField();
		nameField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if (nameField.getText().isEmpty() == ISFALSE && skillPoints == 0) {
					btnMakeCharacter.setEnabled(true);
				}
			}
		});
		horizontalBox.add(nameField);
		nameField.setColumns(FIELD_COLUMNS);

		horizontalBox_1 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_1);

		lblDifficulty = new JLabel("Difficulty");
		lblDifficulty.setHorizontalAlignment(SwingConstants.CENTER);
		horizontalBox_1.add(lblDifficulty);

		horizontalGlue_2 = Box.createHorizontalGlue();
		horizontalBox_1.add(horizontalGlue_2);

		rdbtnBeginner = new JRadioButton("Beginner");
		horizontalBox_1.add(rdbtnBeginner);

		rdbtnEasy = new JRadioButton("Easy");
		horizontalBox_1.add(rdbtnEasy);

		rdbtnNormal = new JRadioButton("Normal");
		rdbtnNormal.setSelected(true);
		horizontalBox_1.add(rdbtnNormal);

		rdbtnHard = new JRadioButton("Hard");
		horizontalBox_1.add(rdbtnHard);

		rdbtnImpossible = new JRadioButton("Impossible");
		horizontalBox_1.add(rdbtnImpossible);
		final ButtonGroup group = new ButtonGroup();
		group.add(rdbtnBeginner);
		group.add(rdbtnEasy);
		group.add(rdbtnNormal);
		group.add(rdbtnHard);
		group.add(rdbtnImpossible);

		horizontalBox_4 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_4);

		lblSkills = new JLabel("Skill Points: ");
		horizontalBox_4.add(lblSkills);

		skillPointLabels = new JLabel("" + skillPoints);
		horizontalBox_4.add(skillPointLabels);

		horizontalGlue_4 = Box.createHorizontalGlue();
		horizontalBox_4.add(horizontalGlue_4);

		horizontalBox_3 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_3);

		lblPilot = new JLabel("Pilot");
		horizontalBox_3.add(lblPilot);

		horizontalGlue_3 = Box.createHorizontalGlue();
		horizontalBox_3.add(horizontalGlue_3);

		button = new JButton("-");
		button.addActionListener(new ActionListener() {
			/**
			 * Removes one point from pilot points and adds it back to the skill
			 * point allowance
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (pilotPoints > 0) {
					pilotPoints--;
					skillPoints++;
					pilotPointsLabel.setText("" + pilotPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints != 0) {
					btnMakeCharacter.setEnabled(false);
				}
			}
		});
		horizontalBox_3.add(button);

		pilotPointsLabel = new JLabel("" + pilotPoints);
		horizontalBox_3.add(pilotPointsLabel);

		button_1 = new JButton("+");
		button_1.addActionListener(new ActionListener() {
			/**
			 * Removes one skill point from the available points and adds it to
			 * pilot points
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (skillPoints > 0) {
					pilotPoints++;
					skillPoints--;
					pilotPointsLabel.setText("" + pilotPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints == 0 && nameField.getText().isEmpty() == ISFALSE) {
					btnMakeCharacter.setEnabled(true);
				}
			}
		});
		horizontalBox_3.add(button_1);

		rigidArea = Box.createRigidArea(new Dimension(
				RIGIDDIMENSIONONE, RIGIDDIMENSIONTWO));
		horizontalBox_3.add(rigidArea);

		horizontalBox_7 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_7);

		lblFighter = new JLabel("Fighter");
		horizontalBox_7.add(lblFighter);

		horizontalGlue_5 = Box.createHorizontalGlue();
		horizontalBox_7.add(horizontalGlue_5);

		button_6 = new JButton("-");
		button_6.addActionListener(new ActionListener() {
			/**
			 * Removes one point from fighter points and adds it back to the
			 * skill point allowance
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (fighterPoints > 0) {
					fighterPoints--;
					skillPoints++;
					fighterPointsLabel.setText("" + fighterPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints != 0) {
					btnMakeCharacter.setEnabled(false);
				}
			}
		});
		horizontalBox_7.add(button_6);

		fighterPointsLabel = new JLabel("" + fighterPoints);
		horizontalBox_7.add(fighterPointsLabel);

		button_7 = new JButton("+");
		button_7.addActionListener(new ActionListener() {
			/**
			 * Removes one skill point from the available points and adds it to
			 * fighter points
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (skillPoints > 0) {
					fighterPoints++;
					skillPoints--;
					fighterPointsLabel.setText("" + fighterPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints == 0 && nameField.getText().isEmpty() == ISFALSE) {
					btnMakeCharacter.setEnabled(true);
				}
			}
		});
		horizontalBox_7.add(button_7);

		rigidArea_1 = Box.createRigidArea(new Dimension(
				RIGIDDIMENSIONONE, RIGIDDIMENSIONTWO));
		horizontalBox_7.add(rigidArea_1);

		horizontalBox_6 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_6);

		lblTrader = new JLabel("Trader");
		horizontalBox_6.add(lblTrader);

		horizontalGlue_6 = Box.createHorizontalGlue();
		horizontalBox_6.add(horizontalGlue_6);

		button_4 = new JButton("-");
		button_4.addActionListener(new ActionListener() {
			/**
			 * Removes one point from trader points and adds it back to the
			 * skill point allowance
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (traderPoints > 0) {
					traderPoints--;
					skillPoints++;
					traderPointsLabel.setText("" + traderPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints != 0) {
					btnMakeCharacter.setEnabled(false);
				}
			}
		});
		horizontalBox_6.add(button_4);

		traderPointsLabel = new JLabel("" + traderPoints);
		horizontalBox_6.add(traderPointsLabel);

		button_5 = new JButton("+");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/**
				 * Removes one skill point from the available points and adds it
				 * to trader points
				 */
				if (skillPoints > 0) {
					traderPoints++;
					skillPoints--;
					traderPointsLabel.setText("" + traderPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints == 0 && nameField.getText().isEmpty() == ISFALSE) {
					btnMakeCharacter.setEnabled(true);
				}
			}
		});
		horizontalBox_6.add(button_5);

		rigidArea_2 = Box.createRigidArea(new Dimension(
				RIGIDDIMENSIONONE, RIGIDDIMENSIONTWO));
		horizontalBox_6.add(rigidArea_2);

		horizontalBox_5 = Box.createHorizontalBox();
		verticalBox.add(horizontalBox_5);

		lblEngineer = new JLabel("Engineer");
		horizontalBox_5.add(lblEngineer);

		horizontalGlue_7 = Box.createHorizontalGlue();
		horizontalBox_5.add(horizontalGlue_7);

		button_2 = new JButton("-");
		button_2.addActionListener(new ActionListener() {
			/**
			 * Removes one point from engineer points and adds it back to the
			 * skill point allowance
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (engineerPoints > 0) {
					engineerPoints--;
					skillPoints++;
					engineerPointsLabel.setText("" + engineerPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints != 0) {
					btnMakeCharacter.setEnabled(false);
				}
			}
		});
		horizontalBox_5.add(button_2);

		engineerPointsLabel = new JLabel("" + engineerPoints);
		horizontalBox_5.add(engineerPointsLabel);

		button_3 = new JButton("+");
		button_3.addActionListener(new ActionListener() {
			/**
			 * Removes one skill point from the available points and adds it to
			 * engineer points
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				if (skillPoints > 0) {
					engineerPoints++;
					skillPoints--;
					engineerPointsLabel.setText("" + engineerPoints);
					skillPointLabels.setText("" + skillPoints);
				}
				if (skillPoints == 0 && nameField.getText().isEmpty() == ISFALSE) {
					btnMakeCharacter.setEnabled(true);
				}
			}
		});
		horizontalBox_5.add(button_3);

		rigidArea_3 = Box.createRigidArea(new Dimension(
				RIGIDDIMENSIONONE, RIGIDDIMENSIONTWO));
		horizontalBox_5.add(rigidArea_3);

		btnMakeCharacter = new JButton("Make Character");
		btnMakeCharacter.addActionListener(new ActionListener() {
			/**
			 * Verifies that a name has been entered, a difficulty has been
			 * selected, and all skill points have been assigned, and then
			 * creates a new Player object using that data.
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				boolean hasName = !nameField.getText().isEmpty();
				boolean hasPoints = skillPoints == 0;
				boolean hasDifficulty = (rdbtnBeginner.isSelected()
						|| rdbtnEasy.isSelected() || rdbtnHard.isSelected()
						|| rdbtnImpossible.isSelected() || rdbtnNormal
						.isSelected());
				if (hasName && hasPoints && hasDifficulty) {
					player = new Player(nameField.getText(), engineerPoints,
							fighterPoints, pilotPoints, traderPoints, STARTING_CREDITS);
					gui.startGame(player);
					// gui; //needs to be second
					gui.updateSolarSystem();
					gui.getChartPanel().updateRange();
					gui.getChartPanel().repaint();
					frame.dispose();
				} else {
					String message = "";
					if (!hasName) {
						message = message + "Please enter your name.";
					}
					if (!hasPoints) {
						message = message + "\nPlease verify that you" +
								" have spent your skill points.";
					}
					if (!hasDifficulty) {
						message = message + "\nPlease select a difficulty";
					JOptionPane.showMessageDialog(panel, message);
					}
				}
			}
		});
		btnMakeCharacter.setEnabled(false);
		frame.getContentPane().add(btnMakeCharacter);
	}

}
